<?php

include_once 'templates/global/agent-login-prevent.php';
include_once '../web.php';
require_once '../Controller/agentManagementController.php';
$agentManagementController = new agentManagementController();
require_once '../Model/Classes/PHPExcel.php';
include_once '../Model/DAL/Functions.php';
$func = new Functions();


$action = "";
$tab = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = (isset($_POST['action']) != NULL) ? $_POST['action'] : '';
    $tab = (isset($_POST['tab']) != NULL) ? $_POST['tab'] : '';
} else {
    $action = (isset($_GET['action']) != NULL) ? $_GET['action'] : '';
    $tab = (isset($_GET['tab']) != NULL) ? $_GET['tab'] : '';
}

// Khi chua chọn tab
if ($tab === "") {
    include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
}

// Khi chọn tab Lịch sử người chơi
else if ($tab === "historyofbet") {
    if ($action === "" || $action === "historyofbet") {
        $result = $agentManagementController->getAllReportOfPlayer();
        $limit = $result["Limit"];
        $range = $result["Range"];
        $start = $result["Start"];
        $total = $result["Total"];
        $arr = $result["Data"];
        include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
    } else if ($action === "historyofbetwithrangedate") {
        $result = $agentManagementController->showHistoryReport();
        $limit = $result["Limit"];
        $range = $result["Range"];
        $start = $result["Start"];
        $total = $result["Total"];
        $arr = $result["Data"];
        include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
    } else if ($action === "exportexcel") {
        $arr = $_SESSION["arrDataExport"];
        $objPHPExcel = new PHPExcel();
        $arr = $_SESSION["arrDataExport"];
        $sumBetMoney = 0;
        $sumPendding = 0;
        $noPending = 0;
        $noWin = 0;
        $noLose = 0;
        $moneyWin = 0;
        $moneyLose = 0;
        //Title
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', 'STT')
                ->setCellValue('B1', 'HỌ')
                ->setCellValue('C1', 'TÊN')
                ->setCellValue('D1', 'MIỀN')
                ->setCellValue('E1', 'LOẠI ĐỀ')
                ->setCellValue('F1', 'TỈNH')
                ->setCellValue('G1', 'SỐ ĐÁNH')
                ->setCellValue('H1', 'SỐ TIỀN ĐÁNH')
                ->setCellValue('I1', 'KẾT QUẢ')
                ->setCellValue('J1', 'SỐ TIỀN THU VỀ');
        //set header
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('J1')->getFont()->setBold(true);
        //set width
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(16);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(16);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(14);


        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->getStyle('G')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        $sheet->getStyle('J')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
        $sheet->getStyle('H')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

        for ($i = 0; $i < count($arr); $i++) {

            if ($arr[$i]['Pendding'] == 0) {
                $noPending ++;
            } else {
                if ($arr[$i]['WinTimes'] == 0) {
                    $noLose++;
                    $moneyWin = $moneyWin + $arr[$i]['BetMoney'];
                } else {
                    $noWin++;
                    $moneyLose = $moneyLose + $arr[$i]['BetMoney'] * $arr[$i]['WinTimes'] * $arr[$i]['WinRateBet'];
                }
            }
            $objPHPExcel->getActiveSheet()->setCellValueExplicit('G' . ($i + 2), $arr[$i]['BetNumber'], PHPExcel_Cell_DataType::TYPE_STRING);
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A' . ($i + 2), $i + 1)
                    ->setCellValue('B' . ($i + 2), $arr[$i]['LastName'])
                    ->setCellValue('C' . ($i + 2), $arr[$i]['FirstName'])
                    ->setCellValue('D' . ($i + 2), $arr[$i]['Mien'])
                    ->setCellValue('E' . ($i + 2), $arr[$i]['Loai'])
                    ->setCellValue('F' . ($i + 2), $arr[$i]['Tinh'])
                    ->setCellValue('H' . ($i + 2), number_format($arr[$i]['BetMoney'], "0", ",", ","))
                    ->setCellValue('I' . ($i + 2), ($arr[$i]['Pendding'] != 0) ? (($arr[$i]['WinTimes'] != 0) ? 'Thắng' : 'Thua') : 'Chưa có kết quả')
                    ->setCellValue('J' . ($i + 2), ($arr[$i]['Pendding'] != 0) ? (($arr[$i]['WinTimes'] == 0) ? '+' . number_format($arr[$i]['BetMoney'], "0", ",", ",") : '-' . number_format($arr[$i]['BetMoney'] * $arr[$i]['WinTimes'] * $arr[$i]['WinRateBet'], "0", ",", ",") ) : 0);

            $sumBetMoney = $sumBetMoney + $arr[$i]['BetMoney'];
            $sumPendding = $sumPendding + $arr[$i]['Pendding'];
        }

        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('I' . (count($arr) + 3), 'Tổng số tiền đánh')
                ->setCellValue('J' . (count($arr) + 3), number_format($sumBetMoney, "0", ",", ","));

        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('I' . (count($arr) + 4), 'Lợi nhuận')
                ->setCellValue('J' . (count($arr) + 4), number_format($moneyWin - $moneyLose, "0", ",", ","));
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('I' . (count($arr) + 5), 'Số lượt chờ KQ')
                ->setCellValue('J' . (count($arr) + 5), $sumPendding - 1);




        $objPHPExcel->getActiveSheet()->getStyle('I' . (count($arr) + 3))->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('J' . (count($arr) + 3))->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('I' . (count($arr) + 4))->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('J' . (count($arr) + 4))->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('I' . (count($arr) + 5))->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('J' . (count($arr) + 5))->getFont()->setBold(true);

        $objPHPExcel->getActiveSheet()->setTitle('Báo Cáo');
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="baocao.xls"');
        ob_end_clean();
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }
}

// Khi chọn tab Lịch sử người chơi Thắng
else if ($tab === "historyofbetwin") {
    if ($action === "" || $action === "historyofbetwin") {
        $result = $agentManagementController->getTopUserWin();
        $limit = $result["Limit"];
        $range = $result["Range"];
        $start = $result["Start"];
        $total = $result["Total"];
        $arr = $result["Data"];
        include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
    }
}

// Khi chọn tab Lịch sử người chơi Thua
else if ($tab === "historyofbetlose") {
    if ($action === "" || $action === "historyofbetlose") {
        $result = $agentManagementController->getTopUserLose();
        $limit = $result["Limit"];
        $range = $result["Range"];
        $start = $result["Start"];
        $total = $result["Total"];
        $arr = $result["Data"];
        include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
    }
}


// Khi chọn tab Doanh Thu Ngày
else if ($tab === "revenusday") {
    if ($action === "" || $action === "revenusday") {
        $arr = $agentManagementController->chartRevenusDayByDay();
        include_once 'templates/managements/agent/statistics/ct-revenus-day.php';
    }
}

// Khi chọn tab Doanh Thu Ngày theo Số ngày
else if ($tab === "revenusdaybynoday") {
    if ($action === "" || $action === "revenusdaybynoday") {
        $arr = $agentManagementController->chartRevenusDayByNoDay();
        include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
    } else if ($action === "revenusdaybynoday_ajax") {
        $arr = $agentManagementController->chartRevenusDayByNoDay_Ajax();
        include_once 'templates/managements/agent/statistics/ct-revenus-day.php';
    }
}

// Khi chọn tab Doanh Thu Tuần theo Số tuần
else if ($tab === "revenusweekbynoweek") {
    if ($action === "" || $action === "revenusweekbynoweek") {
        $arr = $agentManagementController->chartRevenusWeekByNoDay();
        include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
    } else if ($action === "revenusweekbynoweek_ajax") {
        $arr = $agentManagementController->chartRevenusWeekByNoDay_Ajax();
        include_once 'templates/managements/agent/statistics/ct-revenus-week.php';
    }
}

// Khi chọn tab Doanh Thu Năm theo Số năm
else if ($tab === "revenusyearbynoyear") {
    if ($action === "" || $action === "revenusyearbynoyear") {
        $arr = $agentManagementController->chartRevenusYearWithMonthByNoYear();
        include_once 'templates/managements/agent/tmp-agent-management-thongke.php';
    } else if ($action === "revenusyearbynoyear_ajax") {
        $arr = $agentManagementController->chartRevenusYearWithMonthByNoYear_Ajax();
        include_once 'templates/managements/agent/statistics/ct-revenus-year.php';
    }
}